/*
 *所有关于tb_project_fundingsubject_realition类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*tb_project_fundingsubject_realitionService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using MYData.Entity.DomainModels;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using MYData.System.IRepositories;
using System.Collections.Generic;
using Mapster;
using MYData.System.IServices;
using System;
using MYData.Entity.DTO.ApiEntity.Input;
using MYData.Core.EFDbContext;
using System.Linq;
using MYData.Core.Utilities;
using MYData.Entity.DTO.ApiEntity.OutPut;

namespace MYData.System.Services
{
    public partial class tb_project_fundingsubject_realitionService : Itb_project_fundingsubject_realitionService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Itb_project_fundingsubject_realitionRepository _repository;//访问数据库
        private readonly VOLContext _db;
        [ActivatorUtilitiesConstructor]
        public tb_project_fundingsubject_realitionService(
            Itb_project_fundingsubject_realitionRepository dbRepository,
            IHttpContextAccessor httpContextAccessor,
            VOLContext db
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            _db = db;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }
        WebResponseContent webResponse = new WebResponseContent();

        /// <summary>
        /// 项目添加(暂存)
        /// </summary>
        /// <param name="list"></param>
        public void AddListTs(IEnumerable<ProjectFundingItemInput_Dto> list, string projectId)
        {
            var model = list.Adapt<IEnumerable<tb_project_fundingsubject_realition>>();
            foreach (var item in model)
            {
                //var key= new Snowflake.Core.IdWorker(1, 1).NextId().ToString();
                var key = Guid.NewGuid().ToString();
                item.Id = key;
                item.projectId = projectId;
                _repository.Add(item, false);
            }
        }

        /// <summary>
        /// 项目修改(暂存)
        /// </summary>
        /// <param name="list"></param>
        public void UptListTs(IEnumerable<ProjectFundingItemUpdate_Dto> list, string projectId)
        {
            var model = list.Adapt<IEnumerable<tb_project_fundingsubject_realition>>();
            var DelList1 = _db.Set<tb_project_fundingsubject_realition>().Where(x => x.projectId == projectId).ToList();
            //单个删除(清空表数据)
            foreach (var item1 in DelList1)
            {
                _repository.Delete(item1, false);
            }
            foreach (var item in model)
            {
                //var key= new Snowflake.Core.IdWorker(1, 1).NextId().ToString();
                var key = Guid.NewGuid().ToString();
                item.Id = key;
                item.projectId = projectId;
                _repository.Add(item, false);
            }

        }

        /// <summary>
        /// 项目经费添加
        /// </summary>
        /// <param name="list"></param>
        public void AddList(List<tb_project_fundingsubject_realition_Dto> list)
        {
            var model = list.Adapt<List<tb_project_fundingsubject_realition>>();
            _repository.AddRange(model, true);
        }

        /// <summary>
        /// 项目经费修改
        /// </summary>
        /// <param name="list"></param>
        public void UptList(List<tb_project_fundingsubject_realition_Dtotwo> list)
        {
            var model = list.Adapt<List<tb_project_fundingsubject_realition>>();
            _repository.UpdateRange(model, true);
        }

        /// <summary>
        /// 根据项目Id 获取该项目下科目报销信息
        /// MJZ 4/10/17:01
        /// </summary>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public WebResponseContent GetFundingSubject_RelationList(string projectId)
        {
            var list = from x in _db.Set<tb_project_fundingsubject_realition>()
                       join y in _db.Set<Tb_Project_Fundtemplate_Item>() on x.subjectId equals y.budgetItemId
                       where y.partentId == "0" && x.projectId.Equals(projectId) && y.budgetiState.Equals(0) && y.isDelete==0
                       select new FundingSubject_RelationOutputDto
                       {
                           budgetiState = y.budgetiState,
                           projectId = x.projectId,
                           partentId = y.partentId,
                           budgetitemName = y.budgetitemName,
                           budgetType = y.budgetType,
                           directFunding = x.directFunding,
                           overheadFunding = x.overheadFunding,
                           subjectId = x.subjectId,
                           templateId = x.templateId,
                       };
            return webResponse.OK("科目报销信息获取成功", list);
        }
    }
}
